package com.lottery.im.util;

import android.content.ContentResolver;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {
  /**
   * @return 获取当前时间
   */
  public static Date getCurDate() {
    return new Date();
  }

  /**
   * @return 获取本周第一天
   */
  public static Date getFirstDayOfWeek() {
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
    return calendar.getTime();
  }

  /**
   * @return 获取上周第一天
   */
  public static Date getFirstDayOfLastWeek() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    calendar.add(Calendar.WEEK_OF_MONTH, -1);
    return calendar.getTime();
  }

  /**
   * @return 获取上周最后一天
   */
  public static Date getLastDayOfLastWeek() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    return calendar.getTime();
  }

  /**
   * @return 获取本月第一天
   */
  public static Date getFirstDayOfMonth() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    return calendar.getTime();
  }

  /**
   * @return 获取上月第一天
   */
  public static Date getFirstDayOfLastMonth() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.add(Calendar.MONTH, -1);
    return calendar.getTime();
  }

  /**
   * @return 获取上月最后一天
   */
  public static Date getLastDayOfLastMonth() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.add(Calendar.DATE, -1);
    return calendar.getTime();
  }

  /**
   * @return 获取前一天
   */
  public static Date getLastDay() {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, -1);
    return calendar.getTime();
  }

  /**
   * @param date 需要格式化的日期
   * @return 返回格式化后的结果
   */
  public static String DateFormat(Date date) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String format = sdf.format(date);
    return format;
  }

  public static String dateFormat(String date) {
    SimpleDateFormat format0 = new SimpleDateFormat("yyyyMMddHHmmss");
    try {
      Date parse = format0.parse(date);
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String format = sdf.format(parse);
      return format;
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return "1970-01-01 00:00:00";
  }

  public static int getCurrentMonthLastDay(int index) {
    Calendar a = Calendar.getInstance();
    a.add(Calendar.MONTH, index);
    a.set(Calendar.DATE, 1);//把日期设置为当月第一天
    a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
    int maxDate = a.get(Calendar.DATE);
    return maxDate;
  }

  public static String getCurrentYearAndMonth(int index) {
    Calendar a = Calendar.getInstance();
    a.add(Calendar.MONTH, index);
    int year = a.get(Calendar.YEAR);
    int month = a.get(Calendar.MONTH) + 1;
    return year + "年" + month + "月";
  }

  public static String getCurrentYearAndMonth2(int index) {
    Calendar a = Calendar.getInstance();
    a.add(Calendar.MONTH, index);
    int year = a.get(Calendar.YEAR);
    int month = a.get(Calendar.MONTH) + 1;
    return year + "" + (month < 10 ? "0" + month : month);
  }

  public static int getFirstDayOfMonth(int index) {
    Calendar a = Calendar.getInstance();
    a.add(Calendar.MONTH, index);
    a.set(Calendar.DAY_OF_MONTH, 1);
    int i = a.get(Calendar.DAY_OF_WEEK);
    return i;
  }

  public static final SimpleDateFormat DEFAULT_SDF = new SimpleDateFormat("yyyy-MM-dd", Locale
      .getDefault());

  public static final SimpleDateFormat
      DEFAULT_ALL_SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale
      .getDefault());
  public static final SimpleDateFormat DEFAULT_ALL_SDF_EMPTY =
      new SimpleDateFormat("yyyyMMddHHmmss", Locale
          .getDefault());

  /**
   * Milliseconds to the formatted time string.
   *
   * @param millis The milliseconds.
   * @param format The format.
   * @return the formatted time string
   */
  public static String millis2String(long millis, @NonNull final DateFormat format) {
    return format.format(new Date(millis));
  }

  public static long string2Millis(final String time, @NonNull final DateFormat format) {
    try {
      return format.parse(time).getTime();
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return -1;
  }

  /**
   * 将时间字符串转为Date类型
   * <p>格式为用户自定义</p>
   *
   * @param time 时间字符串
   * @param format 时间格式
   * @return Date类型
   */
  public static Date string2Date(String time, SimpleDateFormat format) {
    return new Date(string2Milliseconds(time, format));
  }

  /**
   * 将Date类型转为时间字符串
   * <p>格式为用户自定义</p>
   *
   * @param time Date类型时间
   * @param format 时间格式
   * @return 时间字符串
   */
  public static String date2String(Date time, SimpleDateFormat format) {
    return format.format(time);
  }

  public static Date getOneMoney() {
    Calendar c = Calendar.getInstance();
    c.setTime(new Date());
    c.add(Calendar.MONTH, -1);
    Date m = c.getTime();
    return m;
  }

  public static Date getOneWeek() {
    Calendar c = Calendar.getInstance();
    c.setTime(new Date());
    c.add(Calendar.WEEK_OF_YEAR, -1);
    Date m = c.getTime();
    return m;
  }

  /**
   * 将时间字符串转为时间戳
   * <p>格式为用户自定义</p>
   *
   * @param time 时间字符串
   * @param format 时间格式
   * @return 毫秒时间戳
   */
  public static long string2Milliseconds(String time, SimpleDateFormat format) {
    try {
      return format.parse(time).getTime();
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return -1;
  }

  public static String milliseconds2String(long milliseconds, SimpleDateFormat format) {
    Date d = new Date(milliseconds);
    return date2String(d, format);
  }

  // 获取当前时间戳
  public static long getNowLongTime() {
    return Calendar.getInstance().getTimeInMillis();
  }

  // 获取当前 "yyyy-MM-dd HH:mm:ss 时间
  public static String getNowStringTime() {
    Date date = new Date();
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    return format.format(date);
  }

  // 获取当前 "yyyy-MM-dd HH:mm:ss 时间
  public static String getNowStringDate() {
    Date date = new Date();
    DateFormat format = new SimpleDateFormat("MM-dd");

    return format.format(date);
  }

  public static int differentDaysByData(String date1, String date2) {

    int days = (int) ((string2Date(date1, DEFAULT_ALL_SDF).getTime() - string2Date(date2,
        DEFAULT_ALL_SDF).getTime()) / (1000 * 3600 * 24));
    return days;
  }

  public static int differentDaysByMillisecond(Date date1, Date date2) {
    int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
    return days;
  }

  // 获取当前 "yyyy-MM-dd HH:mm 时间
  public static String getNowMsgStringTime() {
    Date date = new Date();
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    return format.format(date);
  }

  /**
   * 时间转化为星期
   *
   * @param indexOfWeek 星期的第几天
   */
  public static String getWeekDayStr(int indexOfWeek) {
    String weekDayStr = "";
    switch (indexOfWeek) {
      case 1:
        weekDayStr = "星期日";
        break;
      case 2:
        weekDayStr = "星期一";
        break;
      case 3:
        weekDayStr = "星期二";
        break;
      case 4:
        weekDayStr = "星期三";
        break;
      case 5:
        weekDayStr = "星期四";
        break;
      case 6:
        weekDayStr = "星期五";
        break;
      case 7:
        weekDayStr = "星期六";
        break;
    }
    return weekDayStr;
  }

  /**
   * 将时间戳格式化，13位的转为10位
   */
  public static long timestampFormate(long timestamp) {
    String timestampStr = timestamp + "";
    if (timestampStr.length() == 13) {
      timestamp = timestamp / 1000;
    }
    return timestamp;
  }

  /**
   * 获取日起时间秒差
   *
   * @param time 需要格式化的时间 如"2014-07-14 19:01:45"
   * @param pattern 输入参数time的时间格式 如:"yyyy-MM-dd HH:mm:ss"
   * <p/>
   * 如果为空则默认使用"yyyy-MM-dd HH:mm:ss"格式
   * @return time为null，或者时间格式不匹配，输出空字符""
   * @throws ParseException
   */
  public static long formatLongTime(String time, String pattern) {
    long dTime = 0;
    if (time != null) {
      if (pattern == null) {
        pattern = "yyyy-MM-dd HH:mm:ss";
      }
      Date tDate;
      try {
        tDate = new SimpleDateFormat(pattern).parse(time);
        Date today = new Date();
        if (tDate != null) {
          dTime = (today.getTime() - tDate.getTime()) / 1000;
        }
      } catch (ParseException e) {
        e.printStackTrace();
      }
    }
    return dTime;
  }

  /**
   * 获取日期时间戳
   *
   * @param time 需要格式化的时间 如"2014-07-14 19:01:45"
   * @param pattern 输入参数time的时间格式 如:"yyyy-MM-dd HH:mm:ss"
   * <p/>
   * 如果为空则默认使用"yyyy-MM-dd HH:mm:ss"格式
   * @return time为null，或者时间格式不匹配，输出空字符""
   * @throws ParseException
   */
  public static long timeStamp(String time, String pattern) {
    long dTime = 0;
    if (time != null) {
      if (pattern == null) {
        pattern = "yyyy-MM-dd HH:mm:ss";
      }
      Date tDate;
      try {
        tDate = new SimpleDateFormat(pattern).parse(time);
        if (tDate != null) {
          dTime = tDate.getTime() / 1000;
        }
      } catch (ParseException e) {
        e.printStackTrace();
      }
    }
    return dTime;
  }

  /**
   * 时间转化为显示字符串
   *
   * @param timeStamp 单位为秒
   */
  public static String getTimeStr(long timeStamp) {
    if (timeStamp == 0) {
      return "";
    }
    Calendar inputTime = Calendar.getInstance();
    inputTime.setTimeInMillis(timeStamp * 1000);
    Date currenTimeZone = inputTime.getTime();
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    if (calendar.before(inputTime)) {
      SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
      return sdf.format(currenTimeZone);
    }
    calendar.add(Calendar.DAY_OF_MONTH, -1);
    if (calendar.before(inputTime)) {
      return "昨天";
    } else {
      calendar.add(Calendar.DAY_OF_MONTH, -5);
      if (calendar.before(inputTime)) {
        return getWeekDayStr(inputTime.get(Calendar.DAY_OF_WEEK));
      } else {
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        int year = inputTime.get(Calendar.YEAR);
        int month = inputTime.get(Calendar.MONTH);
        int day = inputTime.get(Calendar.DAY_OF_MONTH);
        return year + "/" + month + "/" + day;
      }
    }
  }

  public static boolean is24(Context context) {
    ContentResolver cv = context.getContentResolver();
    String strTimeFormat = android.provider.Settings.System.getString(cv,
        android.provider.Settings.System.TIME_12_24);

    if (strTimeFormat.equals("24")) {
      return true;
    }
    return false;
  }

  /**
   * 20180909121212 类型时间转 2018-09-09
   */
  public static String strAllTime2Format(String time) {

    if (TextUtils.isEmpty(time)) {
      return "";
    }
    if (time.length() == 14 || time.length() == 8) {
      StringBuilder newTime = new StringBuilder();
      newTime.append(time.substring(0, 4));
      newTime.append("-");
      newTime.append(time.substring(4, 6));
      newTime.append("-");
      newTime.append(time.substring(6, 8));
      return newTime.toString();
    } else {
      return "";
    }
  }

  /**
   * 20180909121212 类型时间转 2018-09-09 12:12:12
   */
  public static String strAllTime2AllFormat(String time) {
    if (TextUtils.isEmpty(time)) {
      return "";
    }
    if (time.length() < 14) return "";
    StringBuilder newTime = new StringBuilder();
    newTime.append(time.substring(0, 4));
    newTime.append("-");
    newTime.append(time.substring(4, 6));
    newTime.append("-");
    newTime.append(time.substring(6, 8));
    newTime.append(" ");
    newTime.append(time.substring(8, 10));
    newTime.append(":");
    newTime.append(time.substring(10, 12));
    newTime.append(":");
    newTime.append(time.substring(12, 14));
    return newTime.toString();
  }
}
